package cn.pengpeng.day05.mywc;

import java.io.IOException;

import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class Wc {
	
	public static class WcMapper extends Mapper<LongWritable, Text, Bean, NullWritable>{
		
		public WcMapper() {
			System.out.println("WcMapper类被构造了...............................");
		}
		
		@Override
		protected void setup(Mapper<LongWritable, Text, Bean, NullWritable>.Context context)
				throws IOException, InterruptedException {
			System.out.println("WcMapper类的setup()方法执行了.......................");
		}
		
		
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Bean, NullWritable>.Context context)
				throws IOException, InterruptedException {
			System.out.println("WcMapper类的map()方法执行了......................");
			Bean b = new Bean();
			String[] split = value.toString().split(" ");
			for (String w : split) {
				b.set(w, 1);
				context.write(b, NullWritable.get());
			}
		}
		
		
		@Override
		protected void cleanup(Mapper<LongWritable, Text, Bean, NullWritable>.Context context)
				throws IOException, InterruptedException {
			System.out.println("WcMapper类的cleanup()方法执行了.......................");
		}
		
	}

	
	public static class WcReducer extends Reducer<Bean, NullWritable, Bean, NullWritable>{
		
		@Override
		protected void reduce(Bean bean, Iterable<NullWritable> nulls,
				Reducer<Bean, NullWritable, Bean, NullWritable>.Context context) throws IOException, InterruptedException {
			int count = 0;
			for (NullWritable nullWritable : nulls) {
				count += bean.getCount();
			}
			Bean b = new Bean();
			b.set(bean.getWord(), count);
			context.write(b, NullWritable.get());
			
		}
		
		
	}
	
	
	public static void main(String[] args) throws Exception {
		
		Configuration conf = new Configuration();
		
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(Wc.class);
		job.setMapperClass(WcMapper.class);
		job.setReducerClass(WcReducer.class);
		
		job.setMapOutputKeyClass(Bean.class);
		job.setMapOutputValueClass(NullWritable.class);
		
		job.setOutputKeyClass(Bean.class);
		job.setOutputValueClass(NullWritable.class);
		
		job.setPartitionerClass(WordPartitioner.class);
		job.setGroupingComparatorClass(WcGcp.class);
		job.setCombinerClass(WcCombiner.class);
		
		FileInputFormat.setInputPaths(job, new Path("f:/mrdata/wordcount/input/"));
		FileOutputFormat.setOutputPath(job, new Path("f:/mrdata/wordcount/output/"));
		
		job.setNumReduceTasks(2);
		
		boolean res = job.waitForCompletion(true);
	}
	
}
